package smap

import (
	"fmt"
	"reflect"
	"sync"
)

type TestMap struct {
	m         sync.Map
	keyType   reflect.Type
	valueType reflect.Type
}

func (cMap *TestMap) Load(key interface{}) (value interface{}, ok bool) {
	if reflect.TypeOf(key) != cMap.keyType {
		return
	}
	return cMap.m.Load(key)
}

func (cMap *TestMap) Store(key, value interface{}) error {
	if reflect.TypeOf(key) != cMap.keyType {
		return fmt.Errorf("wrong key type: %v", reflect.TypeOf(key))
	}
	if reflect.TypeOf(value) != cMap.valueType {
		return fmt.Errorf("wrong value type: %v", reflect.TypeOf(value))
	}
	cMap.m.Store(key, value)
	return nil
}

func NewConcurrentMap(key, value interface{}) *TestMap {
	keyType := reflect.TypeOf(key)
	valueType := reflect.TypeOf(value)
	return &TestMap{
		keyType:   keyType,
		valueType: valueType,
	}
}
